﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

using GetPermutationsOrCombinatons.Models;

using Microsoft.Win32;

namespace NumberCheck.ViewModels;

public partial class MainWindowViewModel : ObservableObject
{
    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(CalcCommand))]
    private string _excelpath = "";

    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(CalcCommand))]
    private string _input = "";

    [ObservableProperty]
    private List<string>? _output;

    [ObservableProperty]
    private double[] _sources = [1, 2, 3, 4, 5];

    public bool CanCalc => !String.IsNullOrEmpty(Input) && !String.IsNullOrEmpty(Excelpath);

    [RelayCommand(CanExecute = nameof(CanCalc))]
    public void Calc()
    {
        for (int i = 1; i < Sources.Length + 1; i++)
        {
            var lists = PermutationAndCombination<double>.GetCombination(Sources, i);

            foreach (double[] item in lists!)
            {
                string total = item.Sum().ToString();

                if (total == Input)
                {
                    Console.WriteLine(String.Join("+", item), total);
                }
            }
        }

        Console.WriteLine("未找到！");
    }

    [RelayCommand]
    public void Read()
    {
        var openFile = new OpenFileDialog { Filter = "Excel文件 (.xlsx)|*.xlsx" };

        bool? result = openFile.ShowDialog();

        if (result == true)
        {
            Excelpath = openFile.FileName;
        }
    }
}
